/*
 * dialog 1.0 
 *
 * dialog extension for xWindow - Plugin for jQuery
 *
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Copyright (c) 2008 Oleg Slobodskoi (ajaxsoft.de)
 *
 */

(function($){
	
	var d = {
		draggable: false,
		minimizable: false,
		maximizable: false,
		statusbar: false,
		modal: true,
		content: null,
		height: 130,
		buttonOk: "Ok",
		buttonAbort: "Abort", 
		timeout: 2000,
		opacity: 0.8,
		bgiframe: true
	};
	
	
	/**************************************************************************/
	//Dialogs
	
	$.xWindow.prototype.confirm = function (options, callback )
	{
		var d1 = $.extend({},d, options);
		$.extend(d1, {
			extension: 'confirm',
			buttons: {
				ok: {
					title: d1.buttonOk,
					focus: true,
					onclick: function(){
						this.destroy();
						callback();
					}
				},
				abort: {
					title: d1.buttonAbort,
					onclick: function(){
						this.destroy();
					}
				}
	
			},
			title: d1.title || 'Confirm',
			content: d1.content
			
	
		});
	
		this.defaults(d1);
		this.init();
		
	};				
	
	$.xWindow.prototype.prompt = function(options, callback)
	{
		var d1 = $.extend({},d, options);
		$.extend(d1,	{
				height: 150,
				extension: 'prompt',
				buttons: {
					ok: {
						title: d1.buttonOk,
						focus: true,
						onclick: function(){
							this.createCallback(callback,[this.$elem.children('input').val(), this.$elem]);
						}
					},
					abort: {
						title: d1.buttonAbort,
						onclick: function(){
							this.destroy();
						}
					}
	
				},
				title: d1.title || 'Prompt',
				content: '<p>'+d1.content+'</p><p class="error"></p><input type="text" />'
		});
		
		this.$elem.children('input').focus().keydown(function(e) {
			e.keyCode && e.keyCode == 13 && $(this).parent().parent().find('.focus').click() && $(this).unbind('keydown');
		});
		
		var self = this;
		this.$elem.bind('error', function(e,error){
			var height = $(this).children('.error').text(error).height();
			self.size({height: 150+height});
		});
			
		this.defaults(d1);
		this.init();
		
	};		
	
	/**************************************************************************/
	//Monologs		
	
	$.xWindow.prototype.alert = function (options, callback)
	{
		var d1 = $.extend({},d, options);
		$.extend(d1,	{
			extension: 'alert',
			onclose: callback,
			buttons: {
				ok: {
					title: d1.buttonOk,
					focus: true,
					onclick: function(){
						callback();
						this.destroy();
					}
				}
			},
			title: d1.title || 'Alert',
			content: d1.content
		});
		
		this.defaults(d1);
		this.init();
	};
	
	$.xWindow.prototype.bubble = function(options, callback)
	{

		var timeout;
		var d1 = $.extend({},d, options);
		$.extend(d1,	{
			extension: 'bubble',
			modal: false,
			titlebar: false,
			statusbar: false,
			position: {
				right: 5,
				top: 5					
			},
			animate: true,
			onClose: function(){
				clearTimeout(timeout);
			}
		});
		this.$elem.click(this.destroy);
		
		timeout = setTimeout(this.destroy, d1.timeout);		
			
		this.defaults(d1);
		this.init();
		
	};	
	
	$.xWindow.prototype.humanizedMessage = function(options, callback)
	{

		var d1 = $.extend({},d, options);

		function fadeOut()
		{
			$elem.fadeOut(d.speed, function(){
				$elem.remove();
			}).unbind('click');
		};
		

		var $elem = $('<div class="humanized-message"/>').
			css({
				opacity: d.opacity,
				top: 100 + $(document).scrollTop()
			}).
			text(d1.content).click(fadeOut).
			appendTo('body');
		
		$('<div class="corner-lt"/><div class="corner-rt"/><div class="corner-lb"/><div class="corner-rb"/>').
		appendTo($elem);
		
		d1.bgiframe && $.fn.bgiframe && $elem.bgiframe();
		
		$elem.fadeIn(d.speed, function(){
			setTimeout(fadeOut, d1.timeout);
		});
		
		
	};		




})(jQuery);		